{ ---------------------------------------------------------------------
Title: ACME Drum Kit
File: ACME Drum Kit.txt
Author: David Healey
First Written: January 05 2014
Current Version: 1.0
Last Modified: January 05 2014
----------------------------------------------------------------------}

on init   

    make_perfview {Display script tab in UI}

    set_ui_height_px(250) {Set the height of the UI}

    SET_CONDITION(NO_SYS_SCRIPT_RLS_TRIG) {Disable Kontakt Release Triggers}


    {* Arrays *}
    declare %dynamic_1[3] := (0, 1, 2)
    declare %dynamic_2[3] := (3, 4, 5)
    declare !note_names[128]

    declare !notes[12] {Note name strings}

        !notes[0] := "C"
        !notes[1] := "C#"
        !notes[2] := "D"
        !notes[3] := "D#"
        !notes[4] := "E"
        !notes[5] := "F"
        !notes[6] := "F#"
        !notes[7] := "G"
        !notes[8] := "G#"
        !notes[9] := "A"
        !notes[10] := "A#"
        !notes[11] := "B"         

    {* Constants *}

    declare const $RELEASE_GROUP := 6 {Release sample group}
    declare const $NUM_REPS := 3 {Number of round robins}
    declare const $OPENHH_KEY := 54
    declare const $CLOSEHH_KEY := 56
   
    {* Variables *}
    declare $rr_rep := 0
    declare $openhh_id
    declare $release_id
    declare $i := 0 {Loop counter (incrementer)}

    {* Populate !note_names string array with all the note names of the keyboard *}
    while ($i < 128) {While loop}
        !note_names[$i] := !notes[$i mod 12] & (($i/12)-2) {Populate !note_names}
        inc($i) {Increment counter}
    end while

    
    message("")

end on

on note   

    disallow_group($ALL_GROUPS) {Disable all groups}

    reset_rls_trig_counter(EVENT_NOTE) {Reset the release trigger counter}

    if ($EVENT_VELOCITY < 65) {If the played velocity is less than 65}
        allow_group(%dynamic_1[$rr_rep]) {Enable dynamic 1 group with next repetition}
    end if   

    if ($EVENT_VELOCITY > 64) {If the played velocity is greater than 64}
        allow_group(%dynamic_2[$rr_rep]) {Enable dynamic 2 group with next repetition}
    end if    

    if ($EVENT_NOTE = $OPENHH_KEY) {If open hi-hat is played}

        ignore_event($EVENT_ID) {Ignore the event, we"re going to handle this manually}        
        fade_out($openhh_id, 100000, 1) {Fade out previous open hi-hat note}
        $openhh_id := play_note($EVENT_NOTE, $EVENT_VELOCITY, 0, -1) {Play the open hi-hat and save the note"s ID in $openhh_note}
        
    end if

  

    if ($EVENT_NOTE = $CLOSEHH_KEY) {If closed hi-hat is played}
        fade_out($openhh_id, 100000, 1) {Fade out open hi-hat}
    end if       

    $rr_rep := ($rr_rep + 1) mod $NUM_REPS {Calculate next round robin step - mod is molulo (returns the remainder)}   

    message(...
    	!note_names[$EVENT_NOTE] & ": " & $EVENT_NOTE) {Output the note number of the played note - commands can be split into multiple lines using ...}

end on

on release   

    disallow_group($ALL_GROUPS)
    allow_group($RELEASE_GROUP) {Enable the release group}
    
    fade_out($release_id, 100000, 1) {Fade out previous release note}
    $release_id := play_note($EVENT_NOTE, $EVENT_VELOCITY, 0, -1) {Play the release note}    

end on